package com.example.casper.Experiment2024.Activity;

import android.content.Intent;
import android.os.Bundle;
import android.widget.Button;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import com.example.casper.Experiment2024.Database.AppDatabase;
import com.example.casper.Experiment2024.Database.GeoPoints;
import com.example.casper.Experiment2024.Database.PunchRecord;
import com.example.casper.Experiment2024.R;

import java.util.ArrayList;
import java.util.List;
import com.google.android.material.floatingactionbutton.FloatingActionButton;

public class ManagePunchActivity extends AppCompatActivity {
    private RecyclerView recyclerView;
    private PunchRecordAdapter adapter;
    private List<PunchRecord> punchRecordList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_manage_punch);
        recyclerView = findViewById(R.id.recycler_view);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        loadPunchRecords();

        // 添加打卡记录按钮
        FloatingActionButton addPunchButton = findViewById(R.id.add_punch_button); // 修改为 FloatingActionButton
        addPunchButton.setOnClickListener(v -> {
            Intent intent = new Intent(ManagePunchActivity.this, AddClockInActivity.class);
            startActivity(intent);
        });

        // 足迹按钮
        Button footprintButton = findViewById(R.id.footprint_button);
        footprintButton.setOnClickListener(v -> {
            Intent intent = new Intent(ManagePunchActivity.this, TencentMapActivity.class);
            // 获取所有打卡记录的经纬度
            AppDatabase db = AppDatabase.getInstance(this);
            List<PunchRecord> punchRecords = db.punchRecordDao().getAll();

            ArrayList<Double> latitudes = new ArrayList<>();
            ArrayList<Double> longitudes = new ArrayList<>();

            for (PunchRecord record : punchRecords) {
                latitudes.add(record.getLatitude());
                longitudes.add(record.getLongitude());
            }

            // 将经纬度传递给 TencentMapActivity
            GeoPoints geoPoints = new GeoPoints(latitudes, longitudes);
            intent.putExtra("geo_points", geoPoints); // 使用自定义的 Serializable 对象
            startActivity(intent);
        });

        // 导入按钮
        Button importButton = findViewById(R.id.import_button);
        importButton.setOnClickListener(v -> {
            importPunchRecords();
        });
    }

    private void loadPunchRecords() {
        AppDatabase db = AppDatabase.getInstance(this);
        punchRecordList = db.punchRecordDao().getAll();
        adapter = new PunchRecordAdapter(punchRecordList, new PunchRecordAdapter.OnItemClickListener() {
            @Override
            public void onViewClick(PunchRecord record) {
                Intent intent = new Intent(ManagePunchActivity.this, PunchDetailActivity.class);
                intent.putExtra("record_id",(long) record.getId());
                startActivity(intent);
            }

            @Override
            public void onDeleteClick(PunchRecord record) {
                deletePunchRecord(record);
            }
        });
        recyclerView.setAdapter(adapter);
    }

    private void deletePunchRecord(PunchRecord record) {
        AppDatabase db = AppDatabase.getInstance(this);
        db.punchRecordDao().delete(record); // 假设你在 DAO 中实现了 delete 方法
        loadPunchRecords();
        Toast.makeText(this, "打卡记录已删除", Toast.LENGTH_SHORT).show();
    }

    private void importPunchRecords() {
        // TODO: 实现导入打卡记录的功能
        Toast.makeText(this, "导入打卡记录功能尚未实现", Toast.LENGTH_SHORT).show();
    }

    private void exportPunchRecords() {
        // TODO: 实现导出打卡记录的功能
        Toast.makeText(this, "导出打卡记录功能尚未实现", Toast.LENGTH_SHORT).show();
    }
}